package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.HandleAlertDataUpdate;
import com.zzyl.nursing.dto.AlerDataDto;
import com.zzyl.nursing.dto.QueryAlerDataDto;
import com.zzyl.nursing.vo.QueryAlertDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    @Override
    public TableDataInfo queryAlerDataDto(QueryAlerDataDto alertDataDto) {

        QueryAlerDataDto alertData1 = new QueryAlerDataDto();
        BeanUtils.copyProperties(alertDataDto,alertData1);
        DateTime startTime = DateUtil.parse(alertDataDto.getStartTime());
        DateTime endTime = DateUtil.parse(alertDataDto.getEndTime());

        Long userId = SecurityUtils.getUserId();
        //使用MP进行分页
        IPage<AlertData> page = new Page<>(alertDataDto.getPageNum(),alertDataDto.getPageSize());
        LambdaQueryWrapper<AlertData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(alertDataDto.getDeviceName()),AlertData::getDeviceName, alertDataDto.getDeviceName());
        queryWrapper.between(ObjectUtil.isNotNull(alertDataDto.getStartTime())&&ObjectUtil.isNotNull(alertDataDto.getEndTime()),AlertData::getCreateTime,startTime,endTime);
        queryWrapper.eq(ObjectUtil.isNotNull(alertDataDto.getId()),AlertData::getId, alertDataDto.getId());
        queryWrapper.eq(ObjectUtil.isNotNull(alertDataDto.getStatus()),AlertData::getStatus, alertDataDto.getStatus());

        // &&!SysUser.isAdmin(userId)
        queryWrapper.eq(ObjectUtil.isNotNull(userId),AlertData::getUserId, userId);
        page = this.page(page, queryWrapper);
        return getDataTable(page);
    }

    protected TableDataInfo getDataTable(IPage page)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        rspData.setCode(200);
        rspData.setMsg("请求成功");
        return rspData;
    }



    @Override
    public void handleAlertData(AlerDataDto alertData) {

        HandleAlertDataUpdate iotId = alertDataMapper.getAlertReason(alertData.getId());
        Long userId = SecurityUtils.getUserId();
        String username = alertDataMapper.getUser(userId);
        DateTime dateTime = DateUtil.parseDate(alertData.getProcessingTime());
        LambdaUpdateWrapper<AlertData> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AlertData::getIotId,iotId.getIotId());
        updateWrapper.eq(AlertData::getAlertRuleId,iotId.getAlertRuleId());
        updateWrapper.eq(AlertData::getFunctionId,iotId.getFunctionId());
        updateWrapper.set(AlertData::getProcessingTime,dateTime);
        updateWrapper.set(AlertData::getProcessingResult,alertData.getProcessingResult());
        updateWrapper.set(AlertData::getStatus,1);
        updateWrapper.set(AlertData::getProcessorId,userId);
        updateWrapper.set(AlertData::getProcessorName,username);
        this.update(updateWrapper);
    }

}
